﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class MessageService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Message> _messageRep;
    public MessageService(SqlSugarRepository<Message> messageRep)
    {
        _messageRep = messageRep;
    }

    /// <summary>
    /// 分页查询信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询信息")]
    public async Task<SqlSugarPagedList<MessageOutput>> Page(PageMessageInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _messageRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.Title.Contains(input.SearchKey)
                || u.Extent.Contains(input.SearchKey)
                || u.Content.Contains(input.SearchKey)
                || u.Switch.Contains(input.SearchKey)
                || u.Number.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.Title), u => u.Title.Contains(input.Title.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Extent), u => u.Extent.Contains(input.Extent.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Content), u => u.Content.Contains(input.Content.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Switch), u => u.Switch.Contains(input.Switch.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Number), u => u.Number.Contains(input.Number.Trim()))
            .WhereIF(input.classify>0, u => u.classify == input.classify)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<Category>((u, classify) => u.classify == classify.Id )
            .Select((u, classify) => new MessageOutput
            {
                Id = u.Id,
                Title = u.Title,
                Extent = u.Extent,
                Content = u.Content,
                Switch = u.Switch,
                Number = u.Number,
                CreateOrgId = u.CreateOrgId,
                CreateOrgName = u.CreateOrgName,
                classify = u.classify, 
                classifyCategoryName = classify.CategoryName,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                IsDelete = u.IsDelete,
            });
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加信息")]
    public async Task<long> Add(AddMessageInput input)
    {
        var entity = input.Adapt<Message>();
        await _messageRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除信息")]
    public async Task Delete(DeleteMessageInput input)
    {
        var entity = await _messageRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _messageRep.FakeDeleteAsync(entity);   //假删除
        //await _messageRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新信息")]
    public async Task Update(UpdateMessageInput input)
    {
        var entity = input.Adapt<Message>();
        await _messageRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取信息")]
    public async Task<Message> Detail([FromQuery] QueryByIdMessageInput input)
    {
        return await _messageRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取信息列表")]
    public async Task<List<MessageOutput>> List([FromQuery] PageMessageInput input)
    {
        return await _messageRep.AsQueryable().Select<MessageOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取信息分类列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "CategoryclassifyDropdown"), HttpGet]
    [DisplayName("获取信息分类列表")]
    public async Task<dynamic> CategoryclassifyDropdown()
    {
        return await _messageRep.Context.Queryable<Category>()
                .Select(u => new
                {
                    Label = u.CategoryName,
                    Value = u.Id
                }
                ).ToListAsync();
    }




}
